package com.example.demo.controller;

import com.example.demo.constant.Constant;
import com.example.demo.enums.ResultCode;
import com.example.demo.model.BookInfo;
import com.example.demo.model.RequestPage;
import com.example.demo.model.Result;
import com.example.demo.model.ResultPage;
import com.example.demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        //数据校验
//        if(bookInfo==null) return "bookInfo为null";
        if(!StringUtils.hasLength(bookInfo.getBookName())
                ||!StringUtils.hasLength(bookInfo.getAuthor())
                ||bookInfo.getCount()==null
                ||bookInfo.getPrice()==null
                ||!StringUtils.hasLength(bookInfo.getPublish())
                ||bookInfo.getStatus()==null
        ){
            log.info("传入的数据为空{}",bookInfo);
            return "传入的数据有为空";
        }
        try {
            log.info("添加图书的信息 book: {}",bookInfo);
            return bookService.addBook(bookInfo);
        }catch (Exception e){
            log.error("添加图书失败{}",e);
            return "添加图书失败";
        }
    }
    @RequestMapping("/getListByPage")
    public Result<ResultPage<BookInfo>> getListByPage(RequestPage requestPage){
        //参数校验
        log.info("请求页面：requestPage{}",requestPage);
        if(requestPage==null
                ||requestPage.getPageSize()==null
                ||requestPage.getCurrentPage()==null){
            log.info("参数有为null的");
            return null;
        }
        //检验是否登录了
//        if(!StringUtils.hasLength((String) session.getAttribute(Constant.USER_NAME))){
//            //用户未登录
//            return Result.getUnLogin("用户未进行登录");
//        }
        try{
            ResultPage<BookInfo> data = bookService.getListByPage(requestPage);

            return Result.getSuccess(data);
        }catch (Exception e){
            log.error("e:{}",e);
            return  Result.getException("发生了异常");
        }

    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer id){
        log.info("传入的id :{}",id);
        if(id==null){
            return null;
        }
        try {
            return bookService.queryBookById(id);
        }catch (Exception e){
            log.error("service层返回异常 e{}",e);
            return null;
        }

    }
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("bookInfo:{}",bookInfo);
        if(bookInfo==null){
            return "传入的bookInfo为空";
        }else{
            try{
                Integer result= bookService.updateBook(bookInfo);
                if(result==1){
                    return "";
                }else {
                    return "更新失败";
                }
            }catch (Exception e){
                log.error("service层发生了异常 e{}",e);
                return "service层发生了异常";
            }

        }
    }

    @RequestMapping(value = "/deleteById",produces = "application/json")
    public String deleteById(Integer id){
        log.info("id:{}",id);
        try {
            Integer result = bookService.deleteById(id);
            if(result==1){
                return "";
            }else {
                return "删除失败请联系管理员";
            }
        }catch (Exception e){
            log.error("service 发生了异常e{}",e);
            return null;
        }
    }

    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("ids:{}",ids);
        try{
            if(ids==null||ids.size()==0){
                return "ids为空";
            }
            Integer result=bookService.batchDeleteBook(ids);
            if(result>=1){
                return "";
            }else {
                return "没有要删除的元素";
            }
        }catch (Exception e){
            log.error("e:{}",e);
            return null;
        }
    }

}
